<?php
// +----------------------------------------------------------------------
// | NewThink [ Think More,Think Better! ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2018 http://www.newthink.cc All rights reserved.
// +----------------------------------------------------------------------
// | 版权所有：山西创泰网络科技有限公司
// +----------------------------------------------------------------------
// | Author: JiYun Date:2019-07-24 Time:16:02
// +----------------------------------------------------------------------

namespace app\api\controller;

use app\common\service\WorkingModelService;
use think\Db;

class CompanyIndustry extends BaseController
{
    /**
     * 获取供浆量和循环泵方案值
     * @param WorkingModelService $wms
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function getSlurrySupplyAndPumpGroup(WorkingModelService $wms)
    {
        $param = $this->request->param();
        
        if (empty($param['plan_number']) || empty($param['tsinghua_plan_name']) || empty($param['company_id']) || empty($param['facility_id'])) {
            $this->error('参数错误');
        }
        
        $times         = $this->getControlTime($param['plan_number']);
        $facilityAlias = $this->getFacilityAlias($param['facility_id']);
        
        $data = [];
        if ($facilityAlias == 'SO2') {
            $data = $wms->forecast($param['company_id'], $param['facility_id'], $times['startTime'], $times['endTime'], $param['plan_number'], $param['tsinghua_plan_name']);
            unset($data['PH']);
        }
        
        $this->success('成功', $data);
    }
    
    /**
     * 获取钙硫比和液气比的趋势图
     * @param WorkingModelService $wms
     * @throws \MongoDB\Driver\Exception\Exception
     */
    public function getCalciumSulfurRatioAndLiquidGasRatio(WorkingModelService $wms)
    {
        $param = $this->request->param();
        
        if (empty($param['plan_number']) || empty($param['tsinghua_plan_name']) || empty($param['company_id']) || empty($param['facility_id'])) {
            $this->error('参数错误');
        }
        
        $times         = $this->getControlTime($param['plan_number']);
        $facilityAlias = $this->getFacilityAlias($param['facility_id']);
        $data          = [];
        if ($facilityAlias == 'SO2') {
            #获取方案值钙硫比液气比
            $planCalciumSulfurRatioAndLiquidGasRatio = $wms->reaction($param['company_id'], $param['facility_id'], $times['startTime'], $times['endTime'], $param['plan_number'], $param['tsinghua_plan_name']);
            #获取减排天数量
            $days = floor(abs((strtotime($times['startTime']) - strtotime($times['endTime']))) / (60 * 60 * 24));
            #组合echart格式
            $data = [
                'CaSRatio' => [],
                'liquidGasRatio' => []
            ];
            for ($i = 0; $i <= $days; ++$i) {
                $data['CaSRatio']['date'][]    = date('Y-m-d', strtotime("+{$i} day", strtotime($times['startTime'])));
                $data['CaSRatio']['data'][0][] = $planCalciumSulfurRatioAndLiquidGasRatio['CaSRatio'];
                
                $data['liquidGasRatio']['date'][]    = date('Y-m-d', strtotime("+{$i} day", strtotime($times['startTime'])));
                $data['liquidGasRatio']['data'][0][] = $planCalciumSulfurRatioAndLiquidGasRatio['liquidGasRatio'];
            }
        }
        
        $this->success('成功', $data);
    }
    
    /**
     * 通过预案号获取管控前七天
     * @param $planNumber
     * @return array
     */
    private function getControlTime($planNumber): array
    {
        $sql         = "SELECT `control_start_time`, `control_end_time` FROM `control_plan_name` WHERE `plan_number` = ? ";
        $controlTime = Db::query($sql, array($planNumber));
        if (empty($controlTime)) {
            $this->error('未知预案号!');
        }
        $controlStartTime = strtotime($controlTime[0]['control_start_time']);
        $result           = [
            'startTime' => date('Ymd', strtotime('-7 day', $controlStartTime)),
            'endTime' => date('Ymd', strtotime('-1 day', $controlStartTime)),
        ];
        
        return array_merge($result, $controlTime[0]);
    }
    
    /**
     * 获取治理设施别名 SO2/NO2
     * @param $facilityId
     * @return string
     */
    private function getFacilityAlias($facilityId): string
    {
        $sql      = "SELECT `type` FROM `basic_facility` WHERE id = ?";
        $facility = Db::query($sql, array($facilityId));
        if (empty($facility)) {
            $this->error('未知设施');
        }
        $facilityType = $facility[0]['type'];
        
        $typeAlias = "";
        switch ($facilityType) {
            case 1:
                $typeAlias = "NO2";
                break;
            case 2:
                $typeAlias = "SO2";
                break;
        }
        
        return $typeAlias;
    }
}
